define(['underscore','backbone','./ComponentsJS/modalDialog'],function(_,backbone,modalDialog){
	
	var app = {};
	
    app.Model = Backbone.Model.extend({
    	defaults:{
    		id:"",
    		title:"",
    		completed:false
    	},
    	initialize:function(){
    		
    	},
    	toggle:function(){
    		this.set({
    			completed:!this.get('completed')
    		});
    	}
    });
    
    
    app.Todos = Backbone.Collection.extend({
    	model:app.Model,
    	initialize:function(){
    		//alert("haha");
    	}
    });
    
    var models = [{
    	id:'1',
    	title:'aaaaa',
    	completed:true
    },{
    	id:'2',
    	title:'bbbbbbb',
    	completed:false
    }];
    
    var todos = new app.Todos(models);
    console.log(todos.at(0).get('id'));
    console.log(todos.get('1'));       //根据id
    console.log(todos.where({id:'1'}));     //返回的是数组

    app.todoView = Backbone.View.extend({
    	
    	tagName:"li",
    	
    	//template :$("#todo_tpl").html(),
    	
    	events:{
    		"click .check_single":"toggle"
    	},
    	initialize:function(){
    		this.listenTo(this.model,'change',this.hahah);
    	},
    	render:function(todo){
    		var template = $("#todo_tpl").html();
    		var todofn = _.template(template);
    		
    		//一定要用$el括起来events事件才能绑定
    		var todotpl = this.$el.html(todofn({item:todo.attributes}));    
    		$('#todolis_div ul').append(todotpl);
    	},
    	toggle:function(e){
    		
    		//整个视图this.model就是默认绑定这条数据的
    		console.log(this.model);
    		var id = e.target.getAttribute("data-value");
    	    this.model.toggle();
    	    console.log(todos);
    	},
    	hahah:function(){
    		
    	}
    });
    
    app.appView = Backbone.View.extend({
    	el:"#todos",
    	events:{
    		"click #add_todo_btn":"addOne"   //create--> listenTo(add) --> addOne
    	},
    	initialize:function(){
    		this.listenTo(todos,'add',this.render);     //自动传参todo
    		this.listenTo(todos,'change',this.chg);
    	},
    	addOne:function(){
    		var title = $('#todo_content').val();
    		var id = parseInt(todos.last().get("id"))+1;
    		var dataobj = {
    			id:id,
    			title:title,
    			completed:false
    		};
    		todos.add(dataobj);
             
    	},
    	render:function(todo){
    		var todoview = new app.todoView({
    			model:todo
    		});
    		todoview.render(todo);
    		
    		if($('#todolis_div ul').html()!==""){
    			$(".opertion").css('display','block');
    		}	
    	},
    	chg:function(){
    		alert("A");
    	}
    });

    
	return app;
	
});

//1.没创建todoview
//2.todoview写法问题